home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_200
/
234_01
/
util.c
< prev
next >
Wrap
Text File
|
1987-06-16
|
11KB
|
469 lines
/*
HEADER: CUG nnn.nn;
TITLE: XDIR - Hard Disk Manager
VERSION: 1.0 for IBM-PC
DATE: Apr 03, 1987
DESCRIPTION: Hard Disk Manager for IBM PC
KEYWORDS: Hard Disk Manager Dump Directory
SYSTEM: IBM-PC and Compatiables
FILENAME: util.c
WARNINGS: None
CRC: N/A
SEE-ALSO: HDIR.DOC and XDIR.DOC
AUTHOR: Mike Blakley 15645 SW 82 Cir Ln #76, Miami, Fl 33193
COMPILERS: ECO-C
REFERENCES: XDIR.DOC
*/
/*
utility routines for xdir
xrename - rename a file
xcopy - copy files
xdump - dump a file in hex and ascii
dline - build a dump line in hex and ascii
upmod - change file attributes
getpath - get path name
*/
#include "stdio.h"
#include "dir.h"
#include "xdir.h"
/*
xrename
rename a file
*/
void xrename(old,new)
char *old, *new;
{
int i;
i = rename(old,new);
if (i != 0)
{
writestr("\nCouldn't rename ");
writestr(old);
writestr(" to ");
writestr(new);
writestr(" - press enter ");
getch();
}
}
/*
xcopy
copy a file
*/
#define BUFFSIZE 128
void xcopy(fromf,tof)
char *fromf, *tof;
{
int fdi;
int fdo;
static int i;
static char buffer[BUFFSIZE];
static int numbytes;
fdo = creat(tof,0);
if (fdo == EOF)
{
writestr("\nCan't open output file ");
writestr(tof);
writestr(" - press any key");
getch();
return;
}
fdi = open(fromf,0);
if (fdi == EOF)
{
writestr("\nCan't open input file ");
writestr(fromf);
writestr(" - press any key");
getch();
unlink(tof);
return;
}
while ((numbytes = read(fdi,buffer,BUFFSIZE)) != EOF)
{
if (numbytes < 0)
{
writestr("\nRead error occurred - ");
writestr(fromf);
writestr("press any key ");
getch();
unlink(tof);
return;
}
if (numbytes == 0) break;
i = write(fdo,buffer,numbytes);
if (i <= 0)
{
writestr("\nError writing output file ");
writestr(tof);
writestr(" -press any key");
getch();
unlink(tof);
return;
}
if (numbytes < BUFFSIZE) break;
} /* end while */
i = close(fdi);
if (i == EOF)
{
writestr("\nCouldn't close input ");
writestr(fromf);
writestr(" press any key ");
getch();
}
i = close(fdo);
if (i == EOF)
{
writestr("\nCouldn't close output ");
writestr(tof);
writestr(" press any key ");
getch();
}
} /* end function */
/*
xdump
dump a file
*/
#define DUMPSIZE 256
void xdump(fnam)
char *fnam;
{
int fdi; /* file descriptor */
static int i,j, k, c;
static char buffer[DUMPSIZE];
static int numbytes;
char pline[79];
long offset; /* byte offset of record displayed */
static int currpage, lastpage;
long foff, lseek();
char temp[20];
currpage = 0;
offset = 0L;
fdi = open(fnam,0);
if (fdi == EOF)
{
writestr("\nCan't open input file ");
writestr(fnam);
writestr(" - press any key");
getch();
return;
}
foff = lseek(fdi,0L,2); /* search end of file */
foff /= DUMPSIZE;
lastpage = (int) foff; /* determine last page */
while (1)
{
foff = (long) currpage; /* current page */
foff *= (long) DUMPSIZE;
offset = lseek(fdi,foff,0); /* do a seek to position */
numbytes = read(fdi,buffer,DUMPSIZE);
if (numbytes < 0)
{
writestr("\nRead error occurred - ");
writestr(fnam);
writestr("press any key ");
getch();
return;
}
k = DUMPSIZE - numbytes; /* indicate eof fill pattern */
for (j=0;j<k;j++) buffer[numbytes+j] = 0x1a;
clrscr();
writestr("\n\033[1mDump of file \033[0m");
writestr("\033[7m");
writestr(fnam);
writestr("\033[0m");
writestr("\n\n\n\n");
for (i=0;i<16;i++)
{
j = i * 16;
dline(buffer+j,pline,offset+(long)j); /* build dump line */
putchar('\n');
writestr(pline);
}
writestr("\n\n\n\033[7mEnd of page -\033[0m");
writestr("\033[1mESC\033[0m = Exit PgUp PgDn Home End +n -n F1=Help ");
c = toupper(getch());
if (c == 0) c= getch(); /* pickup up special keys */
if (c == '\033') break;
else
if (c == 73) /* PgUp */ --currpage;
else
if (c == 81) /* PgDn */ ++currpage;
else
if (c == 13) /* CR */ ++currpage;
else
if (c == 79) /* End */ currpage = lastpage;
else
if (c == 71) /* Home */ currpage = 0;
else
if (c == '+')
{
putchar(c);
gets(temp);
currpage += atoi(temp);
}
else
if (c == '-')
{
putchar(c);
gets(temp);
currpage -= atoi(temp);
}
else
if (c == 59) /* Help */
{
clrscr();
writestr(" Hex/Ascii Dump - Help Screen ");
writestr("\n\n\nKey Function");
writestr("\n_____________________________\n");
writestr("\nPgUp Previous Page");
writestr("\nPgDn Next Page ");
writestr("\nHome Restart at beginning ");
writestr("\nEnd Go to End ");
writestr("\nF1 Help - This screen ");
writestr("\n+nnn Forward nnn 256 byte blocks ");
writestr("\n-nnn Backward nnn 256 byte blocks ");
writestr("\n<CR> Next Page ");
writestr("\n\n\nPress any key to continue ");
getch();
}
if (currpage < 1) currpage = 0;
if (currpage > lastpage) currpage = lastpage;
} /* end while */
i = close(fdi);
if (i == EOF)
{
writestr("\nCouldn't close input ");
writestr(fnam);
writestr(" press any key ");
getch();
}
} /* end function */
/*
dline
build a dump line from 16 bytes
*/
void dline(ibuff,xbuff,offset)
char *ibuff, *xbuff;
long offset;
{
int i,j,k,c;
char lbuff[80], rbuff[20], obuff[10];
char *cpl, *cpr;
clear(lbuff,80,' ');
clear(rbuff,20,' ');
cpl = lbuff;
cpr = rbuff;
j = k = (int) offset;
j &= 0xf000;
j >>= 12;
j &= 0x0f;
if (j < 10) obuff[0] = j + '0';
else obuff[0] = j + 'a' - 10;
j = k & 0x0f00;
j >>= 8;
if (j < 10) obuff[1] = j + '0';
else obuff[1] = j + 'a' - 10;
j = k & 0x00f0;
j >>= 4;
if (j < 10) obuff[2] = j + '0';
else obuff[2] = j + 'a' - 10;
j = k & 0x000f;
if (j < 10) obuff[3] = j + '0';
else obuff[3] = j + 'a' - 10;
obuff[4] = 0;
for (i=0;i<16;i++)
{
c = (int) *ibuff++; /* get character */
if ((c >= 0x20) && (c <= 0x7f))
*cpr++ = c;
else *cpr++ = '.';
j = (c & 0xf0);
j >>= 4;
if (j < 10) *cpl++ = j + '0';
else *cpl++ = j + 'a' - 10;
j = (c & 0x0f);
if (j < 10) *cpl++ = j + '0';
else *cpl++ = j + 'a' - 10;
*cpl++ = ' ';
}
*cpl = *cpr = 0;
strcpy(xbuff,obuff);
strcat(xbuff," ");
strcat(xbuff,lbuff);
strcat(xbuff," ");